package xiyuan.keywords.split;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class SplitWord {


    public static List<Word> sentenceToWords(String text){

    	List<Word> list = new LinkedList<Word>();
		//clear out emocons
		text = text.replaceAll("\\[[^\\[]+\\]", " ");
		//clear out characters
		//text = text.replaceAll("[∩╮╯▽╰╭….～/!！⊙﹏□:(']", " ");
		//clear out no-chinese characters
		text  = text.replaceAll("[^\u4e00-\u9fa5]", " ");
		text = text.trim();
		if (text.length() == 0)
			return list;
        Sentence all = null;
		try {
			all = splitWord(text);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //System.out.println(cur.getId() + "\t");
		//dao.getSession().clear();
		if (all == null)
			return list;
        for (int i=0;i<all.totalWords();i++){
            Word word = (Word) all.getWord(i);
            if (!word.getAttribute().matches("(n|nr|nr1|" +
            		"nr2|nrj|ns|nt|wj|nl|ng|v|vd|vn|vl|vg|" +
            		"a|an|ag|al)"))
            	continue;
            list.add(word);
        }
        return list;
    }
	
	public static Sentence splitWord(String sSentence) throws UnsupportedEncodingException {
        Sentence sentence = new Sentence();
        ICTCLAS50 ict = ICTCLAS50.getInstance();
        sSentence = sSentence.trim();
        //String[] arr = sSentence.split("\\s+");
        
        //for (String sen : arr){
	        String str = ict.paragraphProcess(sSentence);
	        //System.out.println(str);
	                
	        String[] allWords = str.split(" ");
	        for (int i = 0; i < allWords.length; i++) {
	            int pos = allWords[i].lastIndexOf("/");
	            if (pos > 0) {
	                Word word = new Word(allWords[i].substring(0, pos),
	                		allWords[i].substring(pos+1));
	                sentence.addWord(word);
	            }
	        }
        //}
        return sentence;
    }
}
